package com.wythe.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wythe.mall.R;
import com.wythe.mall.intf.OnRecyclerViewItemClickListener;
import com.wythe.mall.model.ClassifyBean;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.DPIUtil;

import java.util.ArrayList;


/**
 * @author anumbrella
 * 
 * @date 2015-8-1
 * 
 *       网格view的自定义adapter
 * 
 */
public class GridViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	private ClassifyBean.GoodsVoListBean type;
	private ArrayList<ClassifyBean.GoodsVoListBean> listTypes;
	private Context context;

	public GridViewAdapter(Context mContext, ArrayList<ClassifyBean.GoodsVoListBean> list) {
		this.context = mContext;
		this.listTypes = list;
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gridview, parent, false);
		return new ItemViewHolder(view);
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
		final ItemViewHolder itemHolder = (ItemViewHolder) holder;
		View rootView = itemHolder.itemView.getRootView();
		if (listTypes != null && listTypes.size() > 0) {
			type = listTypes.get(position);
			Glide.with(context).load(type.getIcon()).into(itemHolder.imageView);
			itemHolder.textView.setText(type.getTitle());
			itemHolder.mDes.setText(type.getSkuVos().get(0).getTitle());
			itemHolder.mPrices.setText("￥"+type.getSkuVos().get(0).getPrice());
			rootView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mOnRecyclerViewItemClickListener != null) {
						int layoutPosition = itemHolder.getLayoutPosition();
						mOnRecyclerViewItemClickListener.onRecyclerViewItemClick(v, layoutPosition);
					}
				}
			});
		}
	}

	@Override
	public int getItemCount() {
		return listTypes.size();
	}

	private class ItemViewHolder extends RecyclerView.ViewHolder {
		private TextView textView,mDes,mPrices;
		private ImageView imageView;

		public ItemViewHolder(View view) {
			super(view);
			textView = view.findViewById(R.id.typeName_gridView);
			imageView = view.findViewById(R.id.icon_gridView);
			mDes = view.findViewById(R.id.typeDes_gridView);
			mPrices = view.findViewById(R.id.typePrice_gridView);
		}
	}

	private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;

	public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
		mOnRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
	}

}
